import java.util.Scanner;

/**
 * @Title: 螺旋矩阵
 * @Author 贾金博
 * @Package PACKAGE_NAME
 * @Date 2024/3/8 16:16
 * @description:
 */
public class 螺旋矩阵 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] x = new int[] {0, 1, 0, -1};
        int[] y = new int[] {1, 0, -1, 0};
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] a = new int[n][m];
        boolean[][] flag = new boolean[n][m];
        for(int i = 0; i < n; ++ i ) {
            for(int j = 0; j < m; ++ j ) {
                a[i][j] = sc.nextInt();
            }
        }
        String ret = "";
        int t_x = 0;
        int t_y = 0;
        int sum = 1;
        int t = 0;
        ret += a[0][0] + " ";
        flag[0][0] = true;

        while(sum < n * m ) {
            if(t_x + x[t] < 0 || t_x + x[t] >= n || t_y + y[t] < 0 || t_y + y[t] >= m || flag[t_x + x[t]][t_y + y[t]]) {
//                System.out.println("t_x + x[t] = " + (t_x + x[t]));
//                System.out.println("t_y + y[t] = " + (t_y + y[t]));
                t = (t + 1) % 4;
            } else {
                t_x += x[t];
                t_y += y[t];
                sum++;
                ret += a[t_x][t_y] + " ";
                flag[t_x][t_y] = true;
            }
            //System.out.println("ret = " + ret);
        }
        System.out.println(ret);
    }

}
